<?php
class Country extends Zend_Db_Table_Abstract {
	
	protected $_primary = "id";
	protected $_name = "country";
	
	/**
	 * Find country by id.
	 * 
	 * @param $id
	 */
	public function findById($id) {
		return $this->find($id)->current();
	}
	
	/**
	 * Find country by country code.
	 * 
	 * @param $code unique coountry code
	 */
	public function findByCode($code) {
		$where = $this->select()->where("code=?", $code);
		return $this->fetchRow($where);
	}
	
	/**
	 * Get available countries as selectable array.
	 */
	public static function getAvailableCountriesAsArray() {
		$table = new Country();
		$where = $table->select()->where("enabled=?",1);
		$countries = $table->fetchAll($where);
		$arr = array();
		foreach ($countries as $country) {
			$arr[$country->id] = $country->name_cn;
		}
		return $arr;
	}
	
	/**
	 * Get city by given country id.
	 * 
	 * @param $countryId
	 * @return  return array which can be added to form select
	 */
	public static function getCityByCountryAsArray($countryId) {
		$table = new Country ( );
		$city = new City ( );
		$country = $table->find ( $countryId )->current ();
		$rows = $country->findDependentRowset ( "City", "Country", $city->select ()->where ( "parent_id is null" )->order ( "name ASC" ) );
		$ret = array();
		foreach ( $rows as $row ) {
			$ret [$row->id] = $row->name_cn;
		}
		return $ret;
	}
}
?>